package dbinit;

import java.sql.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import core.profile.model.BasicProfile;
import core.role.model.BasicRoles;
import core.util.HibernateCoreUtil;

public class InitBasicProfile {
	
	public static BasicProfile getBasicProfileByUuid(Long uuid)
	{
		BasicProfile bp = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				bp = (BasicProfile) session.get(BasicProfile.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return bp ;
	}
	public static BasicProfile getBasicProfileByEmail(String email)
	{
		BasicProfile bp = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long uuid = 0L;
		try
		{
			transaction = session.beginTransaction();
			List <BasicProfile> bps = session.createQuery("from BasicProfile b where b.emailId='"+email+"'").list();
			if(bps.size()!= 0)
			{
				 bp = bps.get(0);
			}
			 
			 transaction.commit();
		}
		catch(HibernateException e)
		{
			e.printStackTrace();
		}
		return bp;
	}
	// orgId,departmentIds,basicaddressId,permanentAddressId,dob,"vineet.saxena10@gmail.com",joinD,null,"Saxena","Vineet",true,designationId
	public static Long doinit(Date anniversary,Date birthdaydate,String companyname,String firstname,String gender,int usertype,String lastname,String nickname,String secondname,String title,String businesspartnerno,java.sql.Date creationdate,Boolean status,String emailId,Long currentAddressId,Long permanentAddressId,Set<Long> basicRoles)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		BasicProfile basicprofile = getBasicProfileByEmail(emailId) ;
		if(basicprofile == null)
		{
			basicprofile = new BasicProfile();
			// set emailId
			basicprofile.setEmailId(emailId);
		}
		// set current address
		basicprofile.setCurrentAddress(InitBasicAddress.getBasicAddressByUuid(currentAddressId));
		// set parmanent address
		basicprofile.setPermanentAddress(InitBasicAddress.getBasicAddressByUuid(permanentAddressId));
		// set anniversary
		basicprofile.setAnniversary(anniversary);
		// set birthdaydate
		basicprofile.setBirthdaydate(birthdaydate);
		// set companyname
		basicprofile.setCompanyname(companyname);
		// set firstname
		basicprofile.setFirstname(firstname);
		// set gender
		basicprofile.setGender(gender);
		// set usertype
		basicprofile.setUsertype(usertype);
		// set lastname
		basicprofile.setLastname(lastname);
		// set nickname
		basicprofile.setNickname(nickname);
		// set secondname
		basicprofile.setSecondname(secondname);
		// set title
		basicprofile.setTitle(title);
		// set businesspartnerno
		basicprofile.setBusinesspartnerno(businesspartnerno);
		// set creationdate
		basicprofile.setCreationdate(creationdate);
		// set status
		basicprofile.setStatus(status);
		
		
		// set basic roles
		Iterator<Long> roleItr = basicRoles.iterator();
		Set<BasicRoles> br = new HashSet<BasicRoles>();
		while(roleItr.hasNext())
		{
			br.add(InitBasicRoles.getBasicRolesByUuid(roleItr.next()));
		}
		basicprofile.setBasicRoles(br);
		Long uuid = null;
		try
		{
			transaction = session.beginTransaction();
			session.merge(basicprofile);
			uuid = basicprofile.getUuid();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		return uuid ;
	}

}
